package jianzhu.dao;

import jianzhu.entity.BjEntity;
import jianzhu.entity.JianZhuEntity;
import jianzhu.entity.Page;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import java.util.List;
@Component()
public class JianZhuDao extends HibernateDaoSupport {


    public void save(JianZhuEntity jianZhuEntity) {

      /*  getSessionFactory().openSession().saveOrUpdate(jianZhuEntity);*/
        Session session=getSessionFactory().openSession();
        session.saveOrUpdate(jianZhuEntity);
        session.flush();//
    }
    public int queryCount(String name){
        String sql = "select * from j06jianzhu where name like ? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        //将查询结果转成Map对象的集合
        List<JianZhuEntity> list=  query.list();
        return list.size();
    }
    public List<JianZhuEntity> queryPage(Page page, String name) {

        String sql = "select a.name,a.id,a.url,b.bname from j06jianzhu a,j06jzclazz b where a.bid=b.id and  name like ? order by systime desc limit ?,? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        query.setParameter(1,  page.getBeginIndex());
        query.setParameter(2,  page.getEveryPage());
        query.setResultTransformer(Transformers.aliasToBean(JianZhuEntity.class));
        List<JianZhuEntity> list=  query.list();

        return  list;
    }

    public JianZhuEntity edit(JianZhuEntity jianZhuEntity) {
        Session session= getSessionFactory().openSession();
        return (JianZhuEntity) session.get(JianZhuEntity.class,jianZhuEntity.getId());
    }

    public void del(JianZhuEntity jianZhuEntity) {
        Session session= getSessionFactory().openSession();
        session.delete(jianZhuEntity);
        session.flush();
    }

    public List<BjEntity> bj() {
        String sql = "select * from j06jzclazz  ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(BjEntity.class));
        List<BjEntity> list=  query.list();
        return  list;
    }

}
